#include "BoardFrame.h"

#include "../common/general/Constantes.h"

BoardFrame::BoardFrame() : herramientasFrame("ToolBox"),
						   buttonFrame("Buttons"),
						   miniBarFrame("Mini Bars"),
						   goldenStarFrame("Golden Star"),
						   huecoFrame("Hueco"),
						   tableroFrame("Tablero"),
						   verticalCheckButton("Vertical") {
    targetList.push_back(Gtk::TargetEntry("ITEM"));

    /* Imagenes para los botones */
    Gtk::Image* imagenBotonAzul = 
		manage(new Gtk::Image(RUTA_BLUE_BUTTON));
    Gtk::Image* imagenBotonVerde = 
		manage(new Gtk::Image(RUTA_GREEN_BUTTON));
    Gtk::Image* imagenBotonRojo = 
		manage(new Gtk::Image(RUTA_RED_BUTTON));
    Gtk::Image* imagenBotonVioleta = 
		manage(new Gtk::Image(RUTA_VIOLET_BUTTON));
    Gtk::Image* imagenBotonAmarillo = 
		manage(new Gtk::Image(RUTA_YELLOW_BUTTON));

    /* Botones para la barra de herramientas */
    botonAzul.add_pixlabel(RUTA_BLUE_BUTTON, "");
    botonVerde.add_pixlabel(RUTA_GREEN_BUTTON, "");
    botonRojo.add_pixlabel(RUTA_RED_BUTTON, "");
    botonVioleta.add_pixlabel(RUTA_VIOLET_BUTTON, "");
    botonAmarillo.add_pixlabel(RUTA_YELLOW_BUTTON, "");

	/* Seteo para que se puedan arrastrar */
    botonAzul.drag_source_set(targetList);
    botonVerde.drag_source_set(targetList);
    botonRojo.drag_source_set(targetList);
    botonVioleta.drag_source_set(targetList);
    botonAmarillo.drag_source_set(targetList);

    /* Imagenes para arrastrar */
    botonAzul.drag_source_set_icon(imagenBotonAzul->get_pixbuf());
    botonVerde.drag_source_set_icon(imagenBotonVerde->get_pixbuf());
    botonRojo.drag_source_set_icon(imagenBotonRojo->get_pixbuf());
    botonVioleta.drag_source_set_icon(imagenBotonVioleta->get_pixbuf());
    botonAmarillo.drag_source_set_icon(imagenBotonAmarillo->get_pixbuf());

    /* Layout botones */
    botonesHBox.set_layout(Gtk::BUTTONBOX_SPREAD);
    botonesHBox.pack_start(botonAzul, Gtk::PACK_SHRINK);
    botonesHBox.pack_start(botonVerde, Gtk::PACK_SHRINK);
    botonesHBox.pack_start(botonRojo, Gtk::PACK_SHRINK);
    botonesHBox.pack_start(botonVioleta, Gtk::PACK_SHRINK);
    botonesHBox.pack_start(botonAmarillo, Gtk::PACK_SHRINK);

    /* Imagenes para los minibar */
    Gtk::Image* imagenMiniBarAzul = manage(new Gtk::Image(RUTA_BLUE_MINIBAR_H));
    Gtk::Image* imagenMiniBarVerde = 
		manage(new Gtk::Image(RUTA_GREEN_MINIBAR_H));
    Gtk::Image* imagenMiniBarRojo = manage(new Gtk::Image(RUTA_RED_MINIBAR_H));
    Gtk::Image* imagenMiniBarVioleta = 
        manage(new Gtk::Image(RUTA_VIOLET_MINIBAR_H));
    Gtk::Image* imagenMiniBarAmarillo = 
        manage(new Gtk::Image(RUTA_YELLOW_MINIBAR_H));

    /* Botones para la barra de herramientas */
    miniBarAzul.add_pixlabel(RUTA_BLUE_MINIBAR_H, "");
    miniBarVerde.add_pixlabel(RUTA_GREEN_MINIBAR_H, "");
    miniBarRojo.add_pixlabel(RUTA_RED_MINIBAR_H, "");
    miniBarVioleta.add_pixlabel(RUTA_VIOLET_MINIBAR_H, "");
    miniBarAmarillo.add_pixlabel(RUTA_YELLOW_MINIBAR_H, "");

	/* Seteo para que se puedan arrastrar */
    miniBarAzul.drag_source_set(targetList);
    miniBarVerde.drag_source_set(targetList);
    miniBarRojo.drag_source_set(targetList);
    miniBarVioleta.drag_source_set(targetList);
    miniBarAmarillo.drag_source_set(targetList);

    /* Imagenes para arrastrar */
    miniBarAzul.drag_source_set_icon(imagenMiniBarAzul->get_pixbuf());
    miniBarVerde.drag_source_set_icon(imagenMiniBarVerde->get_pixbuf());
    miniBarRojo.drag_source_set_icon(imagenMiniBarRojo->get_pixbuf());
    miniBarVioleta.drag_source_set_icon(imagenMiniBarVioleta->get_pixbuf());
    miniBarAmarillo.drag_source_set_icon(imagenMiniBarAmarillo->get_pixbuf());

    /* Layout botones */
    miniBarHBox.set_layout(Gtk::BUTTONBOX_SPREAD);
    miniBarHBox.pack_start(miniBarAzul, Gtk::PACK_SHRINK);
    miniBarHBox.pack_start(miniBarVerde, Gtk::PACK_SHRINK);
    miniBarHBox.pack_start(miniBarRojo, Gtk::PACK_SHRINK);
    miniBarHBox.pack_start(miniBarVioleta, Gtk::PACK_SHRINK);
    miniBarHBox.pack_start(miniBarAmarillo, Gtk::PACK_SHRINK);
	
	miniBarVBox.add(miniBarHBox);
	miniBarVBox.add(verticalCheckButton);

    /* Imagen para la goldenstar */
    Gtk::Image* imagenGoldenStar = manage(new Gtk::Image(RUTA_GOLDENSTAR));

	/* Botones para la barra de herramientas */
    goldenStar.add_pixlabel(RUTA_GOLDENSTAR, "");
	
	/* Seteo para que se puedan arrastrar */
    goldenStar.drag_source_set(targetList);

    /* Imagenes para arrastrar */
    goldenStar.drag_source_set_icon(imagenGoldenStar->get_pixbuf());

    /* Layout botones */
    goldenStarHBox.set_layout(Gtk::BUTTONBOX_SPREAD);
    goldenStarHBox.pack_start(goldenStar, Gtk::PACK_SHRINK);

	/* Imagen para los huecos */
    Gtk::Image* imagenHueco = manage(new Gtk::Image(RUTA_HUECO));

	/* Botones para la barra de herramientas */
    hueco.add_pixlabel(RUTA_HUECO, "");

	/* Seteo para que se puedan arrastrar */
    hueco.drag_source_set(targetList);

    /* Imagenes para arrastrar */
    hueco.drag_source_set_icon(imagenHueco->get_pixbuf());

    /* Layout botones */
    huecoHBox.set_layout(Gtk::BUTTONBOX_SPREAD);
    huecoHBox.pack_start(hueco, Gtk::PACK_SHRINK);
	
	/***********************************************************/
	
    /* Agrego los frames */
    buttonFrame.add(botonesHBox);
    miniBarFrame.add(miniBarVBox);
    goldenStarFrame.add(goldenStarHBox);
	huecoFrame.add(huecoHBox);

    herramientasVBox.add(buttonFrame);
    herramientasVBox.add(miniBarFrame);
    herramientasVBox.add(goldenStarFrame);
	herramientasVBox.add(huecoFrame);

    herramientasFrame.add(herramientasVBox);

    tableroHBox.pack_start(tablero, Gtk::PACK_EXPAND_PADDING);
    tableroFrame.add(tableroHBox);

    mainHBox.add(herramientasFrame);
    mainHBox.add(tableroFrame);

    /* Señales */
    botonAzul.signal_drag_data_get().connect(sigc::mem_fun(*this,
            &BoardFrame::dragBlueButton));
    botonVerde.signal_drag_data_get().connect(sigc::mem_fun(*this,
            &BoardFrame::dragGreenButton));
    botonRojo.signal_drag_data_get().connect(sigc::mem_fun(*this,
            &BoardFrame::dragRedButton));
    botonVioleta.signal_drag_data_get().connect(sigc::mem_fun(*this,
            &BoardFrame::dragVioletButton));
    botonAmarillo.signal_drag_data_get().connect(sigc::mem_fun(*this,
            &BoardFrame::dragYellowButton));

    miniBarAzul.signal_drag_data_get().connect(sigc::mem_fun(*this,
            &BoardFrame::dragBlueMiniBar));
    miniBarVerde.signal_drag_data_get().connect(sigc::mem_fun(*this,
            &BoardFrame::dragGreenMiniBar));
    miniBarRojo.signal_drag_data_get().connect(sigc::mem_fun(*this,
            &BoardFrame::dragRedMiniBar));
    miniBarVioleta.signal_drag_data_get().connect(sigc::mem_fun(*this,
            &BoardFrame::dragVioletMiniBar));
    miniBarAmarillo.signal_drag_data_get().connect(sigc::mem_fun(*this,
            &BoardFrame::dragYellowMiniBar));

    goldenStar.signal_drag_data_get().connect(sigc::mem_fun(*this,
            &BoardFrame::dragGoldenStar));

	hueco.signal_drag_data_get().connect(sigc::mem_fun(*this,
            &BoardFrame::dragHueco));
	
    add(mainHBox);
    show_all_children(true);
}

BoardFrame::~BoardFrame() {
}

Board *BoardFrame::getBoard() {
    return &tablero;
}

void BoardFrame::dragBlueButton(const Glib::RefPtr<Gdk::DragContext> &context,
                             Gtk::SelectionData &selection_data, guint info,
                             guint time) {
    selection_data.set(selection_data.get_target(), 8, 
                      (const guchar *) BLUE_BUTTON, 10);
}

void BoardFrame::dragGreenButton(const Glib::RefPtr<Gdk::DragContext> &context,
                                Gtk::SelectionData &selection_data, guint info, 
                                guint time) {
    selection_data.set(selection_data.get_target(), 8,
                       (const guchar *) GREEN_BUTTON, 11);
}

void BoardFrame::dragRedButton(const Glib::RefPtr<Gdk::DragContext> &context,
                             Gtk::SelectionData &selection_data, guint info, 
                             guint time) {
    selection_data.set(selection_data.get_target(), 8, 
                      (const guchar *) RED_BUTTON,  9);
}

void BoardFrame::dragVioletButton(const Glib::RefPtr<Gdk::DragContext> &context,
                                Gtk::SelectionData &selection_data, guint info,
                                 guint time) {
    selection_data.set(selection_data.get_target(), 8,
                       (const guchar *) VIOLET_BUTTON, 12);
}

void BoardFrame::dragYellowButton(const Glib::RefPtr<Gdk::DragContext> &context,
                                Gtk::SelectionData &selection_data, guint info, 
                                guint time) {
    selection_data.set(selection_data.get_target(), 8,
                       (const guchar *) YELLOW_BUTTON, 12);
}

void BoardFrame::dragBlueMiniBar(const Glib::RefPtr<Gdk::DragContext> &context,
                                Gtk::SelectionData &selection_data, guint info,
                                guint time) {
    selection_data.set(selection_data.get_target(), 8,
                       (const guchar *) (verticalCheckButton.get_active()?BLUE_MINIBAR_V:BLUE_MINIBAR_H), 12);
}

void BoardFrame::dragGreenMiniBar(const Glib::RefPtr<Gdk::DragContext> &context,
                                Gtk::SelectionData &selection_data, guint info,
                                guint time) {
    selection_data.set(selection_data.get_target(), 8,
                       (const guchar *) (verticalCheckButton.get_active()?GREEN_MINIBAR_V:GREEN_MINIBAR_H), 13);
}

void BoardFrame::dragRedMiniBar(const Glib::RefPtr<Gdk::DragContext> &context,
                                Gtk::SelectionData &selection_data, guint info,
                                guint time) {
    selection_data.set(selection_data.get_target(), 8, 
                        (const guchar *) (verticalCheckButton.get_active()?RED_MINIBAR_V:RED_MINIBAR_H), 11);
}

void BoardFrame::dragVioletMiniBar(const Glib::RefPtr<Gdk::DragContext>
                                 &context, Gtk::SelectionData &selection_data,
                                 guint info, guint time) {
    selection_data.set(selection_data.get_target(), 8,
                       (const guchar *) (verticalCheckButton.get_active()?VIOLET_MINIBAR_V:VIOLET_MINIBAR_H), 14);
}

void BoardFrame::dragYellowMiniBar(const Glib::RefPtr<Gdk::DragContext>
                                &context, Gtk::SelectionData &selection_data,
                                guint info, guint time) {
    selection_data.set(selection_data.get_target(), 8,
                       (const guchar *) (verticalCheckButton.get_active()?YELLOW_MINIBAR_V:YELLOW_MINIBAR_H), 14);
}

void BoardFrame::dragGoldenStar(const Glib::RefPtr<Gdk::DragContext> &context,
                                Gtk::SelectionData &selection_data, guint info,
                                guint time) {
    selection_data.set(selection_data.get_target(), 8, 
                                (const guchar *) GOLDENSTAR, 10);
}

void BoardFrame::dragHueco(const Glib::RefPtr<Gdk::DragContext> &context,
                          Gtk::SelectionData &selection_data, guint info,
                          guint time) {
    selection_data.set(selection_data.get_target(), 8, 
                                (const guchar *) HUECO, 10);
}

BoardConfiguration *BoardFrame::obtenerConfiguracion() {
    BoardConfiguration *bc = new BoardConfiguration();
    bc->setImagenes(tablero.getImagenes());

    return bc;
}
